﻿namespace Panda.Utils
{
    using System;
    using System.Collections.Concurrent;
    using System.Collections.Generic;

    public class PandaListPool<T> : IDisposable
    {
        private static readonly ConcurrentBag<ListWrapper> _pool = new ();

        public static ListWrapper Get()
        {
            if (_pool.TryTake(out var list))
            {
                return list;
            }
            
            return new ListWrapper();
        }

        public void Dispose()
        {
            // If needed, implement any cleanup logic here for the pool itself
        }

        public class ListWrapper : List<T>, IDisposable
        {
            internal ListWrapper()
            {
            }

            public void Dispose()
            {
                Clear();
                _pool.Add(this);
            }
        }
    }

}